package com.jinzhi.jzweb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.service.JazBedNumberService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jinzhi.jzweb.dao.JazHouseTypesDao;
import com.jinzhi.jzweb.service.JazHouseTypesService;
import com.jinzhi.common.base.CoreServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 
 * <pre>
 * 房型表
 * </pre>
 * <small> 2024-07-29 08:50:26 | Long</small>
 */
@Service
public class JazHouseTypesServiceImpl extends CoreServiceImpl<JazHouseTypesDao, JazHouseTypesDO> implements JazHouseTypesService {
    @Autowired
    private JazBedNumberService jazBedNumberService;

    @Override
    @Transactional
    public Result<String> saveHouseType(JazHouseTypesDO jazHouseTypesDO) {
        JazHouseTypesDO jazHouseTypesDO2 = this.selectOne(new EntityWrapper<JazHouseTypesDO>().eq("name", jazHouseTypesDO.getName()).eq("build_number", jazHouseTypesDO.getBuildNumber()).last("limit 1"));
        if (jazHouseTypesDO2!=null){
            return Result.fail("已存在对应的小区和楼栋，请去编辑该楼栋！");
        }
        JazHouseTypesDO jazHouseTypesDO1=new JazHouseTypesDO();
        BeanUtils.copyProperties(jazHouseTypesDO,jazHouseTypesDO1);
        jazHouseTypesDO1.setCreateTime(new Date());
        jazHouseTypesDO1.setStatus(0);
        jazHouseTypesDO1.setCanStay(jazHouseTypesDO.getBedCode().size());
        jazHouseTypesDO1.setStay(0);
        this.insert(jazHouseTypesDO1);
        //床号
        List<JazBedNumberDO> list=new ArrayList<>();
        List<String> bedCode = jazHouseTypesDO.getBedCode();
        Set<String> uniqueItems = new HashSet<>(bedCode);
        if (uniqueItems.size() != bedCode.size()) {
            throw new IllegalArgumentException("床号有重复，不能新增！");
        }
        bedCode.forEach(e->{
            JazBedNumberDO jazBedNumberDO=new JazBedNumberDO();
            jazBedNumberDO.setHouseTypeId(jazHouseTypesDO1.getId());
            jazBedNumberDO.setCode(e);
            jazBedNumberDO.setIsOccupied(1);
            list.add(jazBedNumberDO);
        });
        jazBedNumberService.insertBatch(list);

        return Result.ok();
    }

    @Override
    public Result<Page<JazHouseTypesDO>> selHouseTypeList(String name, String buildNumber, Integer status, int current, int size) {
        Page<JazHouseTypesDO> page = new Page<>(current, size);
        List<JazHouseTypesDO> voList = new ArrayList<>();
        Wrapper<JazHouseTypesDO> wrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            wrapper.like("name", "%" + name + "%");
        }
        if (StringUtils.isNotBlank(buildNumber)) {
            wrapper.like("build_number", "%" + buildNumber + "%");
        }
        if (status!=null&&status!=-1){
            wrapper.eq("status",status);
        }
        wrapper.orderBy("create_time",false);
        Page<JazHouseTypesDO> jazHouseTypesDOPage = this.selectPage(page, wrapper);
        for (JazHouseTypesDO jazHouseTypesDO : jazHouseTypesDOPage.getRecords()) {
            JazHouseTypesDO vo = new JazHouseTypesDO();
            BeanUtils.copyProperties(jazHouseTypesDO, vo);
//            List<JazBedNumberDO> list = jazBedNumberService.selectList(new EntityWrapper<JazBedNumberDO>().eq("house_type_id", jazHouseTypesDO.getId()));
//            List<JazBedNumberDO> collect = list.stream().filter(e -> e.getIsOccupied().equals(0)).collect(Collectors.toList());
//            if (!list.isEmpty()){
//                vo.setStay(collect.size());
//                if (Objects.equals(vo.getCanStay(), vo.getStay())){
//                    vo.setStatus(2);
//                }else if (vo.getStay()==null|| vo.getStay()==0){
//                    vo.setStatus(0);
//                }else if (vo.getCanStay()>vo.getStay()){
//                    vo.setStatus(1);
//                }
//            }
            voList.add(vo);
        }
        jazHouseTypesDOPage.setRecords(voList);
        return Result.ok(jazHouseTypesDOPage);
    }

    @Transactional
    @Override
    public void updateHouseType(JazHouseTypesDO jazHouseTypesDO) {
        JazHouseTypesDO jazHouseTypesDO1 = this.selectById(jazHouseTypesDO.getId());
        BeanUtils.copyProperties(jazHouseTypesDO,jazHouseTypesDO1);
        //删除房型下的所有未入住的床号
        jazBedNumberService.delete(new EntityWrapper<JazBedNumberDO>().eq("house_type_id", jazHouseTypesDO.getId()).eq("is_occupied",1));
        List<JazBedNumberDO> doList1 = jazBedNumberService.selectList(new EntityWrapper<JazBedNumberDO>().eq("house_type_id", jazHouseTypesDO.getId()));
        List<String> bedCode = jazHouseTypesDO.getBedCode();

        //查出已占用的床号
        List<String> collect = doList1.stream().map(JazBedNumberDO::getCode).collect(Collectors.toList());

        Set<String> collectSet = new HashSet<>(collect); // 转换成 set 以提高查找效率

        boolean hasDuplicates = bedCode.stream()
                .anyMatch(collectSet::contains);

        if (hasDuplicates) {
            throw new IllegalArgumentException("存在和已入住的床号相同的床号，不能修改！");
        }
        //然后新增
        List<JazBedNumberDO> list=new ArrayList<>();
        Set<String> uniqueItems = new HashSet<>(bedCode);
        if (uniqueItems.size() != bedCode.size()) {
            throw new IllegalArgumentException("床号有重复，不能新增！");
        }
        bedCode.forEach(e->{
            JazBedNumberDO jazBedNumberDO=new JazBedNumberDO();
            jazBedNumberDO.setHouseTypeId(jazHouseTypesDO1.getId());
            jazBedNumberDO.setCode(e);
            jazBedNumberDO.setIsOccupied(1);
            list.add(jazBedNumberDO);
        });
        jazBedNumberService.insertBatch(list);

        jazHouseTypesDO1.setCanStay(bedCode.size()+collect.size());
        if (bedCode.size()+collect.size()>collect.size()){
            jazHouseTypesDO1.setStatus(1);
        }else if ((bedCode.size()+collect.size()==collect.size())){
            jazHouseTypesDO1.setStatus(2);
        }
        this.updateById(jazHouseTypesDO1);
    }

    @Override
    @Transactional
    public String remove(Long id) {
        List<JazBedNumberDO> list = jazBedNumberService.selectList(new EntityWrapper<JazBedNumberDO>().eq("house_type_id", id).eq("is_occupied", 0));
        if (!list.isEmpty()){
            return "该房型下已有入住的阿姨！";
        }else {
            this.deleteById(id);
            jazBedNumberService.delete(new EntityWrapper<JazBedNumberDO>().eq("house_type_id", id));

            return "";
        }


    }

    @Override
    public List<JazHouseTypesDO> selHouseList() {
        List<JazHouseTypesDO> voList=new ArrayList<>();
        List<JazHouseTypesDO> jazHouseTypesDOS = this.selectList(new EntityWrapper<>());

        for (JazHouseTypesDO jazHouseTypesDO:jazHouseTypesDOS){
            JazHouseTypesDO vo = new JazHouseTypesDO();
            BeanUtils.copyProperties(jazHouseTypesDO, vo);
            List<JazBedNumberDO> list = jazBedNumberService.selectList(new EntityWrapper<JazBedNumberDO>().eq("house_type_id", jazHouseTypesDO.getId()).eq("is_occupied", 1));
            vo.setNumberDOS(list);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public List<JazBedNumberDO> selBedNumberList(Long id) {
        return jazBedNumberService.selectList(new EntityWrapper<JazBedNumberDO>().eq("house_type_id", id).eq("is_occupied", 1));
    }
    @Override
    public List<JazBedNumberDO> selBedNumberListZoo(Long id) {
        return jazBedNumberService.selectList(new EntityWrapper<JazBedNumberDO>().eq("house_type_id", id).eq("is_occupied", 0));
    }
}
